package common.export.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.ss.util.CellRangeAddress;
import org.jxls.area.Area;
import org.jxls.command.AbstractCommand;
import org.jxls.command.Command;
import org.jxls.common.CellRef;
import org.jxls.common.Context;
import org.jxls.common.Size;
import org.jxls.expression.ExpressionEvaluator;
import org.jxls.transform.poi.PoiTransformer;
import org.jxls.util.Util;

/**
 */
public class MergedRegionCommand extends AbstractCommand {
	Area area;
	/**
	 * 合并的开始位置，如果没有设置则取当前命令所在单元格
	 */
	String start;
	/**
	 * 合并的行数
	 */
	String row;
	/**
	 * 针对开始位置的偏移量
	 */
	String rowOffset;
	/**
	 * 合并的列数
	 */
	String col;
	/**
	 * 针对开始位置的偏移量
	 */
	String colOffset;
	/**
	 * 如果设置condtion则当conditon为true时才合并
	 */
	String condtion;

	public String getName() {
		return "mergedRegion";
	}

	public Size applyAt(CellRef cellRef, Context context) {
		Size resultSize = area.applyAt(cellRef, context);
		if (null == row && null == col) {
			return resultSize;
		}
		int width = 1;
		int height = 1;
		ExpressionEvaluator evaluator = getTransformationConfig().getExpressionEvaluator();
		if (StringUtils.isNotBlank(condtion)) {
			if (!Util.isConditionTrue(evaluator, condtion, context)) {
				return resultSize;
			}
		}

		if (StringUtils.isNotBlank(row)) {
			Object conditionResult = evaluator.evaluate(row, context.toMap());
			if (conditionResult instanceof Number) {
				height = ((Number) conditionResult).intValue();
			}
		}
		if (StringUtils.isNotBlank(col)) {
			Object conditionResult = evaluator.evaluate(col, context.toMap());
			if (conditionResult instanceof Number) {
				width = ((Number) conditionResult).intValue();
			}
		}
		height = Math.max(height, 1);
		width = Math.max(width, 1);
		PoiTransformer transformer = (PoiTransformer) area.getTransformer();
		Workbook workbook = transformer.getWorkbook();
		Sheet sheet = workbook.getSheet(cellRef.getSheetName());
		CellAddress startAddress = new CellAddress(cellRef.getRow(), cellRef.getCol());
		if (StringUtils.isNotBlank(start)) {
			startAddress = new CellAddress(start);

		}
		int startRow = startAddress.getRow();
		int startCol = startAddress.getColumn();
		
		if(StringUtils.isNotBlank(rowOffset)) {
			Object conditionResult = evaluator.evaluate(rowOffset, context.toMap());
			if (conditionResult instanceof Number) {
				startRow += ((Number) conditionResult).intValue();
			}
		}
		if(StringUtils.isNotBlank(colOffset)) {
			Object conditionResult = evaluator.evaluate(colOffset, context.toMap());
			if (conditionResult instanceof Number) {
				startCol += ((Number) conditionResult).intValue();
			}
		}
		
		int lastRow = startRow + height - 1;
		int lastCol = startCol + width - 1;

		CellRangeAddress cellRange = new CellRangeAddress(startRow, lastRow, startCol, lastCol);
		sheet.addMergedRegion(cellRange);
		return resultSize;
	}

	@Override
	public Command addArea(Area area) {
		super.addArea(area);
		this.area = area;
		return this;
	}

	public String getStart() {
		return start;
	}

	public void setStart(String start) {
		this.start = start;
	}

	public String getRow() {
		return row;
	}

	public void setRow(String row) {
		this.row = row;
	}

	public String getRowOffset() {
		return rowOffset;
	}

	public void setRowOffset(String rowOffset) {
		this.rowOffset = rowOffset;
	}

	public String getCol() {
		return col;
	}

	public void setCol(String col) {
		this.col = col;
	}

	public String getColOffset() {
		return colOffset;
	}

	public void setColOffset(String colOffset) {
		this.colOffset = colOffset;
	}

	public String getCondtion() {
		return condtion;
	}

	public void setCondtion(String condtion) {
		this.condtion = condtion;
	}

}
